<?php

class paging {

    const rowLimit = 10;
    const linkLimit = 10;

    public $pn;
    public $orderBy = "";
    public $orderSC = "ASC";
    public $rowLimit = paging::rowLimit;
    public $linkLimit = paging::linkLimit;
    public $offset;
    public $limit;
    public $linkCount;
    public $totalCount;
    public $totalCountDisplay = "";
    public $linkAction = "";
    public $linkActionList = "";
    public $onclickFunction = "";
    public $onclickFunctionList = "";
    public $first;
    public $preview;
    public $next;
    public $last;
    public $firstLink;
    public $previewLink;
    public $nextLink;
    public $lastLink;
    public $pageLinkFrom;
    public $pageLinkTo;
    
    function __construct($pn, $linkAction) {
        $this->pn = $pn;
        $this->linkAction = $linkAction;
        $this->setPaging();
    }

    
    public function setPaging() {
        if ($this->pn == "" || $this->pn < 0) {
            $this->pn = 1;
        }
        if ($this->pn == 1) {
            $this->offset = 0;
            $this->limit = $this->rowLimit;
        } else {
            $this->offset = ($this->pn - 1 ) * $this->rowLimit;
            $this->limit = $this->offset + $this->rowLimit;
        }
    }

    public function doPaging() {
        if ($this->totalCount > 0) {
            // no have data
            //count link
            $this->linkCount = ceil($this->totalCount / $this->linkLimit);
            // set default for pn
            if ($this->pn == "" || $this->pn > $this->linkCount || $this->pn <= 0) {
                $this->pn = 1;
            }
            
            if($this->linkCount <= 10){
                $this->first = '';
                $this->last = '';
            }

            if ($this->linkCount == 1) {
                // have one page
                $this->preview = "";
                $this->next = "";
                $this->pageLinkFrom = "";
                $this->pageLinkTo = "";
            } else if ($this->linkCount == 2) {
                // have 2 page
                $this->preview = "";
                $this->next = "";
                if ($this->pn == 1) {
                    $this->first = "";
                } else {
                    // pn = 2 or exception
                    $this->last = "";
                }
                $this->pageLinkFrom = 1;
                $this->pageLinkTo = 2;
            } else if ($this->linkCount == 3) {
                // have 3 page
                $this->first = 1;
                $this->preview = $this->pn - 1;
                $this->next = $this->pn + 1;
                $this->last = 3;
                if ($this->pn == 1) {
                    $this->first = "";
                    $this->preview = "";
                } else if ($this->pn == 2) {
                    // pn = 2
                    $this->preview = "";
                    $this->next = "";
                } else {
                    //pn =3
                    $this->next = "";
                    $this->last = "";
                }
                $this->pageLinkFrom = 1;
                $this->pageLinkTo = 3;
            } else {
                // had more row
                $this->first = 1;
                $this->preview = $this->pn - 1;
                $this->next = $this->pn + 1;
                $this->last = $this->linkCount;
                if ($this->pn == 1) {
                    $this->first = "";
                    $this->preview = "";
                }
                if ($this->pn == 2) {
                    $this->preview = "";
                }
                if ($this->pn == $this->linkCount - 1) {
                    $this->next = "";
                }
                if ($this->pn == $this->linkCount) {
                    $this->last = "";
                    $this->next = "";
                }

                // set page link, offset,
                if ($this->linkCount <= $this->linkLimit) {
                    $this->pageLinkFrom = 1;
                    $this->pageLinkTo = $this->linkCount;
                } else {
                    //
                    if ($this->pn <= intval($this->linkLimit / 2)) {
                        // in begin
                        $this->pageLinkFrom = 1;
                        $this->pageLinkTo = $this->linkLimit;
                    } else {
                        if ($this->pn - intval($this->linkLimit / 2)  + $this->linkLimit <= $this->linkCount) {
                            // in mid
                            $this->pageLinkFrom = $this->pn - intval($this->linkLimit / 2);
                            $this->pageLinkTo = $this->pageLinkFrom + $this->linkLimit;
                        } else {
                            // in the end
                            $this->pageLinkFrom = $this->linkCount - $this->linkLimit;
                            $this->pageLinkTo = $this->linkCount;
                        }
                    }
                }
            }
            
            // set offset and limit
            if (($this->pn * $this->rowLimit) > $this->totalCount) {
                $this->limit = $this->totalCount;
            } else {
                $this->limit = $this->pn * $this->rowLimit;
            }
            if ($this->pn == 1) {
                $this->offset = 1;
            } else {
                $this->offset = (($this->pn - 1) * $this->rowLimit + 1);
            }

            if ($this->onclickFunction != "") {
                //set link action list
                if ($this->first != "") {
                    $this->firstLink = " <a href='#' onclick='return " . $this->onclickFunction . "(1);' >Đầu</a>&nbsp";
                }
                if ($this->preview != "") {
                    $this->previewLink = " <a href='#' onclick='return " . $this->onclickFunction . "(" . $this->preview . ");' >Trước</a>&nbsp";
                }
                if ($this->next != "") {
                    $this->nextLink = " <a href='#' onclick='return " . $this->onclickFunction . "(" . $this->next . ");' >Tiếp</a>&nbsp";
                }
                if ($this->last != "") {
                    $this->lastLink = " <a href='#' onclick='return " . $this->onclickFunction . "(" . $this->last . ");'>Cuối</a>&nbsp";
                }
                if ($this->pageLinkFrom != "") {
                    for ($i = $this->pageLinkFrom; $i <= $this->pageLinkTo; $i++) {
                        if ($this->pn == $i) {
                            $this->onclickFunctionList[] = $i + "";
                        } else {
                            $this->onclickFunctionList[] = " <a href='#' onclick='return " . $this->onclickFunction . "(" . $i . ");'>" . $i . "</a>";
                        }
                    }
                }
            }
            // set totalCount Display ;
            if ($this->totalCount == 1) {
                $this->totalCountDisplay = "1 kết quả";
            } else {
                $this->totalCountDisplay = "From " . $this->offset;
                $this->totalCountDisplay .= " to " . $this->limit;
                $this->totalCountDisplay .= " / " . $this->totalCount;
            }
        } else {
            // no data
        }
    }

}

?>